package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceShadowDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author laoYe
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<Device> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:sync')")
    @ApiOperation(value = "同步华为云平台产品")
    @PostMapping("/syncProductList")
    public R syncProductList(){
        deviceService.syncProductList();
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @ApiOperation(value = "查询所有产品信息")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct(){
        List<ProductVo> list = deviceService.allProduct();
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:register')")
    @Log(title = "注册设备", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public R register(@RequestBody DeviceDto deviceDto){
        deviceService.register(deviceDto);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('nursing:device:one')")
    @ApiOperation(value = "查询设备的详细信息")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getDeviceInfo(@PathVariable String iotId){
        DeviceDetailVo vo = deviceService.getDeviceInfo(iotId);
        return R.ok(vo);
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报的最新数据")
    //ApiImplicitParams描述参数类型为对象，有哪些属性是真正的参数
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public R<List<DeviceShadowDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId) {
        List<DeviceShadowDataVo> voList = deviceService.queryServiceProperties(iotId);
        return R.ok(voList);
    }

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation("通过productKey查询产品的详情")
    public R<List<ServiceCapability>> queryProductInfo(@PathVariable String productKey){
        List<ServiceCapability> list = deviceService.queryProductInfo(productKey);
        return R.ok(list);
    }

}
